﻿using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnDaPPE
{
    public class PlcToPc
    {
        PlcController _plc;

        public PlcToPc(PlcController plcInstance)
        {
            this._plc = plcInstance;
        }
        //扫码开始信号
        private int _alarmCode;
        private int _uploadStartLeft;
        private int _uploadStartRight;
        private int _uploadStartOP20;
        private int _uploadStartOP30;
        private int _uploadStartBox;
        //完成信号
        private short _downloadDoneSignLeft;
        private short _downloadDoneSignRight;
        private short _downloadDoneSignOP20;
        private short _downloadDoneSignOP30;
        //条码读取
        private string _sn_ChannelOP10A;
        private string _sn_ChannelOP10B;
        private string _sn_ChannelOP20;
        private string _sn_ChannelOP30;
        private string _sn_ChannelBox;
        private string _sn_ChannelProductSN;

        //plc心跳信号
        private short PCwatchDog10;
        private short PCwatchDog20;
        private short PCwatchDog30;

        /// <summary>
        /// 报警代码
        /// </summary>
        public int AlarmCode
        {
            get
            {
                _alarmCode = _plc.ReadInt("DB1000.DBW4");

                return _alarmCode;
            }
        }

        public short WatchDog10()
        {
            PCwatchDog10=(short) _plc.ReadInt("DB1001.DBW8");
            return PCwatchDog10;
        }
        public short WatchDog20()
        {
            PCwatchDog20 = (short)_plc.ReadInt("DB1001.DBW8");
            return PCwatchDog20;
        }
        public short WatchDog30()
        {
            PCwatchDog30 = (short)_plc.ReadInt("DB1001.DBW8");
            return PCwatchDog30;
        }

        #region 读取各个工站扫码信号

        /// <summary>
        /// 上传信号OP10站
        /// </summary>
        public int UploadStartOP10Left
        {


            get
            {
                _uploadStartLeft = _plc.ReadInt("DB1000.DBW6");
                return _uploadStartLeft;
            }
            set
            {
                _uploadStartLeft = value;
            }


        }
        public int UploadStartOP10Right
        {
            get
            {
                _uploadStartRight = _plc.ReadInt("DB1000.DBW8");//需要分开
                return _uploadStartRight;
            }
            set
            {
                _uploadStartRight = value;
            }
        }
        /// <summary>
        /// 上传信号OP20站
        /// </summary>
        public int UploadStartOP20
        {
            get
            {
                _uploadStartOP20 = _plc.ReadInt("DB1001.DBW6");
                return _uploadStartOP20;
            }
            set
            {
                _uploadStartOP20 = value;
            }
        }
        /// <summary>
        /// 上传信号OP30站
        /// </summary>
        public int UploadStartOP30
        {
            get
            {
                _uploadStartOP30 = _plc.ReadInt("DB1001.DBW6");
                return _uploadStartOP30;
            }
            set
            {
                _uploadStartOP30 = value;
            }
        }

        /// <summary>
        /// 上传信号OP30站
        /// </summary>
        public int UploadStartBox
        {
            get
            {
                _uploadStartBox = _plc.ReadInt("DB1000.DBW13");
                return _uploadStartBox;
            }
            set
            {
                _uploadStartBox = value;
            }
        }


        #endregion
        /// <summary>
        /// 完成信号op10左
        /// </summary>
        public int UploadDoneSignOP10Left()
        {

            _downloadDoneSignLeft = (short) _plc.ReadInt("DB1000.DBW8");
            return _downloadDoneSignLeft;
           

        }

    /// <summary>
    /// 完成信号op10右
    /// </summary>
    public int UploadDoneSignOP10Right()
        {
           
                _downloadDoneSignRight = (short)_plc.ReadInt("DB1001.DBW10");
                return _downloadDoneSignRight;
            
           
        }
        /// <summary>
        /// 完成信号op20
        /// </summary>
        public int UploadDoneSignOP20()
        {
             _downloadDoneSignOP20 = (short)_plc.ReadInt("DB1001.DBW10");
                return _downloadDoneSignOP20;
           
          
        }
        /// <summary>
        /// 完成信号op30
        /// </summary>
        public int UploadDoneSignOP30()
        {
           
                _downloadDoneSignOP30 =(short) _plc.ReadInt("DB1001.DBW10");
                return _downloadDoneSignOP30;
           
        
        }
        #region 工站读码地址
        /// <summary>
        /// 通道op10左工站 产品ID
        /// </summary>
        public string Sn_ChannelOP10A()
        {

            byte[] buffer = _plc.ReadBytes(DataType.DataBlock, 1000, 14, 40);//第一个字节最大字节，第二个字节当前字节
            if (buffer!=null)
            {
                int length = Convert.ToInt32(buffer[1]);
                _sn_ChannelOP10A = _plc.GetPlcDataString(buffer, length + 2);
                return _sn_ChannelOP10A;
            }

            return null;

        }
        /// <summary>
        ///  通道op10右工站 产品ID
        /// </summary>
        public string Sn_ChannelOP10B()
        {

            byte[] buffer = _plc.ReadBytes(DataType.DataBlock, 1001, 238, 40);
            if (buffer!=null)
            {
                int length = Convert.ToInt32(buffer[1]);
                _sn_ChannelOP10B = _plc.GetPlcDataString(buffer, length + 2);
                return _sn_ChannelOP10B;
            }

            return null;
        }

        /// <summary>
        ///  通道op20工站 产品ID
        /// </summary>
        public string Sn_ChannelOP20()
        {

            byte[] buffer = _plc.ReadBytes(DataType.DataBlock, 1001, 238, 40);
            if (buffer!=null)
            {
                int length = Convert.ToInt32(buffer[1]);
                _sn_ChannelOP20 = _plc.GetPlcDataString(buffer, length + 2);
                return _sn_ChannelOP20;
            }
            return null;

        }
        /// <summary>
        ///  通道op30工站 产品ID
        /// </summary>
        public string Sn_ChannelOP30()
        {
            
            byte[] buffer = _plc.ReadBytes(DataType.DataBlock, 1001, 238, 40);
            if (buffer!=null)
            {
                int length = Convert.ToInt32(buffer[1]);
                _sn_ChannelOP30 = _plc.GetPlcDataString(buffer, length + 2);
                return _sn_ChannelOP30;
            }
            return null;

        }

        /// <summary>
        ///  装箱码 
        /// </summary>
        public string Sn_ChannelBox()
        {

            byte[] buffer = _plc.ReadBytes(DataType.DataBlock, 1000, 782, 40);
            int length = Convert.ToInt32(buffer[1]);
            _sn_ChannelBox = _plc.GetPlcDataString(buffer, length + 2);
            return _sn_ChannelBox;

        }
        /// <summary>
        ///  产品码
        /// </summary>
        public string Sn_ChannelProductSN()
        {

            byte[] buffer = _plc.ReadBytes(DataType.DataBlock, 1000, 782, 40);
            int length = Convert.ToInt32(buffer[1]);
            _sn_ChannelProductSN = _plc.GetPlcDataString(buffer, length + 2);
            return _sn_ChannelProductSN;

        }

        #endregion
    }
}
